package cn.itcast.demo.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.io.File;

public class QueryIndex {

    /**
     * 查询索引库数据
     */
    @Test
    public void queryIndexTest() throws Exception {
        //获取读索引库对象
        DirectoryReader reader = DirectoryReader.open(FSDirectory.open(new File("F:\\index89")));
        //1、获取查询索引库对象
        IndexSearcher indexSearcher = new IndexSearcher(reader);

        //准备搜索条件对象  查询所有文档
        //Query query = new MatchAllDocsQuery();
        //基于词条搜索
       // Query query = new TermQuery(new Term("fileName","传智播客"));
        //模糊查询 * 任意多个字符  ? 任意一个字符
        //WildcardQuery query = new WildcardQuery(new Term("fileName","传智播?"));
        //相似度查询  一般针对英文  修改搜索内容不超过2次还原成词条，即可以匹配搜索
        //FuzzyQuery query = new FuzzyQuery(new Term("fileName","Lucene"));
        //数值范围匹配
        //参数1：查询的域，参数2：起始字节数值，参数3：结束字节数值，参数4：包含起始，参数5：包含结束
        NumericRangeQuery<Long> query = NumericRangeQuery.newLongRange("fileSize", 0L, 50L, true, true);

        //多条件组合查询
       /* BooleanQuery query = new BooleanQuery();
        TermQuery query1 = new TermQuery(new Term("fileName", "传智播客"));
        TermQuery query2 = new TermQuery(new Term("fileName", "阴阳师"));

        //MUST必须满足 SHOULD可以满足 MUST_NOT一定不满足
        query.add(query1, BooleanClause.Occur.MUST);
        query.add(query2, BooleanClause.Occur.MUST_NOT);*/

       //单字段域解析查询
       /* String queryStr="传智播客是一家不错的IT培训机构lucene";
        //使用ik分词器分词  一定与创建索引时使用分词一致
        Analyzer analyzer = new IKAnalyzer();
        QueryParser parser = new QueryParser("fileName",analyzer);

        Query query = parser.parse(queryStr);*/

        //多字段域解析查询
      /*  String queryStr="传智播客是一家不错的IT培训机构lucene";
        //使用ik分词器分词  一定与创建索引时使用分词一致
        Analyzer analyzer = new IKAnalyzer();
        //多字段域解析对象
        MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[]{"fileName","fileContent"},analyzer);
        Query query = parser.parse(queryStr);*/
        //2、基于indexSearcher读取索引库
        //参数一：搜索条件对象  参数二：展示搜索结果数
        TopDocs docs = indexSearcher.search(query, 10);
        System.out.println("满足条件总记录数："+docs.totalHits);
        //文档数组
        ScoreDoc[] scoreDocs = docs.scoreDocs;
        //遍历数组展示数据
        for (ScoreDoc scoreDoc : scoreDocs) {
            System.out.println("文档id"+scoreDoc.doc);
            //基于文档id查询文档对象
            Document document = indexSearcher.doc(scoreDoc.doc);
            //文档内容
            System.out.println("名称"+document.getField("fileName"));
            System.out.println("内容"+document.getField("fileContent"));
            System.out.println("大小"+document.getField("fileSize"));
            System.out.println("路径"+document.getField("filePath"));
        }
    }
}
